#=================================
#2023/9/14更新 已优化颜色 by Ivan Yan
#=============================
library(ggalluvial)
library(ggplot2)
library(viridis)
library(RColorBrewer)
library(reshape2)
library(Cairo)
library(showtext)
df <- read.csv("Applicant_Agent-utf8.csv", header=TRUE,stringsAsFactors = FALSE,check.names = FALSE)
df<-df[1:15,1:16]

df_melt<-melt(df,id.vars = 'Applicant')
colnames(df_melt)<-c('from','to','weight')

df_melt$to<-as.character(df_melt$to)
df_melt$group<-seq(1,nrow(df_melt))
df_melt<-melt(df_melt,id.vars = c('weight','group'),value.name ='Entity',factorsAsStrings=FALSE)

#--------------------------------------排序:纵向桑基图-----------------------------------------------
#对申请人排序
df_sum1<-apply(df[,2:ncol(df)],1,sum)
order1<-sort(df_sum1,index.return=TRUE,decreasing =TRUE)
#对代理所排序
df_sum2<-apply(df[,2:ncol(df)],2,sum)
order2<-sort(df_sum2,index.return=TRUE,decreasing =TRUE)

#按照原始表格先行后列排序
df_melt$Entity<-factor(df_melt$Entity,levels=c(df$Applicant[order1$ix],colnames(df)))
#df_melt$Entity<-factor(df_melt$Entity,levels=df$Applicant[order$ix])
df_melt$variable<-factor(df_melt$variable,levels=c('from','to'))


mycolor1 <- colorRampPalette(brewer.pal(9,"Set1"))(nrow(df))
mycolor2 <- colorRampPalette(brewer.pal(9,'YlGnBu'))(ncol(df)-1)

p1<-ggplot(df_melt,
       aes(x = variable,y = weight,
           stratum = Entity, alluvium = group,
           fill = Entity, label = Entity)) +
  geom_flow(alpha = 0.7,width=0.25,color = "darkgray",size=0.1) +
  geom_stratum(alpha =1,width=0.25) +
  geom_text(stat = "stratum", size = 2,angle=0) +
  scale_fill_manual(values= c(mycolor1,mycolor2))+   #values=mycolor)+
  theme_test()+
  theme(legend.position = "none",
        axis.text.y =element_blank(),
        axis.line = element_blank(),
        axis.ticks =element_blank() )+
  scale_x_discrete(labels = c("from" = "专利申请人","to" = "代理事务所"))

#用Cairo包输出1
CairoPDF("plot_p1.pdf",width = 10, height =10,family = "msyh")
showtext_begin()
p1
showtext_end()
dev.off() #关闭图像设备，同时储存图片



#--------------------------------------排序：横向桑基图-----------------------------------------------
f_sum<-apply(df[,2:ncol(df)],2,sum)
order<-sort(df_sum,index.return=TRUE,decreasing =FALSE)

df_melt$Region<-factor(df_melt$Region,levels=df$Region[order$ix])
df_melt$variable<-factor(df_melt$variable,levels=c('from','to'))

ggplot(df_melt,
       aes(x = variable,y = weight,
           stratum = Region, alluvium = group,
           fill = Region, label = Region)) +
  geom_flow(alpha = 0.7,width=0.25,color = "darkgray") +
  geom_stratum(alpha =1,width=0.25) +
  geom_text(stat = "stratum", size = 3.5,angle=0) +
  scale_fill_manual(values= mycolor)+   #values=mycolor)+
  
  coord_flip() +
  theme_test()+
  theme(legend.position = "none",
        #axis.text.y =element_blank(),
        axis.title.x = element_blank(),
        axis.text.x =element_blank(),
        axis.line = element_blank(),
        axis.ticks =element_blank() )